package com.eoekun.dashboard;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.eoekun.dashboard.dao.dataobject.Dashboard;
import com.eoekun.dashboard.dao.mapper.DashboardMapperExt;
import com.eoekun.dashboard.util.CamelConvertUtil;
import com.eoekun.dashboard.util.ExcelUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DashboardDataInit {
    @Autowired
    private DashboardMapperExt dashboardMapperExt;

    @Test
    public void contextLoads() throws IOException {
        String pathname = "src/test/resources/init-data.xls";
        String json = readData(pathname);
        GsonBuilder gb = new GsonBuilder().setDateFormat("yyyy/MM/dd");
        Gson gson = gb.create();
        List<Dashboard> records =
                gson.fromJson(json, new TypeToken<List<Dashboard>>() {}.getType());
        Comparator<Dashboard> c = new Comparator<Dashboard>() {

            @Override
            public int compare(Dashboard o1, Dashboard o2) {
                int c1 = o1.getMonth().compareTo(o2.getMonth());
                return c1;
            }
        };
        Collections.sort(records, c);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        for (Dashboard record : records) {
            try {
                Date createTime = null;
                if (null == record.getPlanStartDate()) {
                    createTime = sdf.parse(record.getMonth().toString());
                } else {
                    createTime = record.getPlanStartDate();
                }
                Calendar cal = Calendar.getInstance();
                cal.setTime(createTime);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                record.setCreateTime(createTime);
                dashboardMapperExt.insertSelective(record);
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println(JSON.toJSON(record));
                break;
            }
        }
        System.out.println(json);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static String readData(String pathname) {
        List rowline = ExcelUtil.readExcel(new File(pathname), "yyyy-MM-dd");
        int l = rowline.size();
        List<String> index = (List<String>) rowline.get(1);
        List<Map<String, Object>> data = new ArrayList<>();
        for (int i = 2; i < l; i++) {
            List<String> list = (List<String>) rowline.get(i);
            Map<String, Object> e = new HashMap<>();
            for (int j = 0; j < list.size(); j++) {
                Object v = list.get(j);
                if (null != v) {
                    try {
                        v = String.valueOf(v).trim().replaceAll("、", ",").replaceAll("\\\\", "")
                                .replaceAll("／", "/").replaceAll("\\s+", " ");
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }
                if (!StringUtils.isEmpty(v)) {
                    String k = index.get(j);
                    k = CamelConvertUtil.underlineToCamel(k);
                    e.put(k, v);
                }
            }
            data.add(e);
        }
        return JSON.toJSONString(data);
    }

    public static void main(String[] args) {
        String pathname = "src/test/resources/init-data.xls";
        String json = readData(pathname);
        System.out.println(json);
    }
}
